function [act,run_act,layer,allROIact,animalIDtxt,regionIDtxt,adata]=check_md_data(animalID,regionID,vis_output,calc_output)

% num_hom(:,1) = animalID
% num_hom(:,2) = timepoint (-48,...,48)
% num_hom(:,3) = animal DB exp ID
% num_hom(:,4) = ExpID
% num_hom(:,8) = StackID (=ExpID)
% num_hom(:,9) = regionID
adata_dir='N:\AnalyzedData\georg\';
tmp_data_dir='E:\RawData\';
[num_hom,txt_hom]=xlsread('C:\Users\georg\Desktop\MD_exps.xlsx');


if nargin<4
    calc_output=0;
end

curr_ind=find(num_hom(:,1)==animalID&num_hom(:,9)==regionID);
if length(curr_ind)<4
    disp('Region does not exist, or too few data points were acquired - Aborting.')
    act=NaN;
    run_act=NaN;
    layer=NaN;
    allROIact=NaN;
    act_code=NaN;
    animalIDtxt=NaN;
    regionIDtxt=NaN;
    adata=NaN;
    return
end
ExpIDs=num_hom(curr_ind,4);
[ExpIDs,exp_ind]=unique(ExpIDs,'first');
curr_ind=curr_ind(exp_ind);

tpointIDs=num_hom(curr_ind,2);
animalIDtxt=txt_hom{min(find(num_hom(:,1)==animalID))+1,1};
regionIDtxt=txt_hom{min(curr_ind+1),11};
auxIDs=txt_hom(curr_ind,8);
layer=num_hom(min(curr_ind),11);

tpoints=[-48 -24 6 18 24 48];

disp('-----------------')
disp(sprintf('Animal %s (%i) region %i (%s)',animalIDtxt,animalID,regionID,regionIDtxt));
disp('-----------------')


for ind=1:length(ExpIDs)
    [adata_file,mouse_id,date_id]=find_adata_file(ExpIDs(ind),adata_dir);
    adata(ind)=load([adata_dir mouse_id '\' date_id '\' adata_file],'ROIs','template','act_map','fnames');
    disp(sprintf('t: %i \t expID: %i \t # ROIs: %i - %i',num_hom(curr_ind(ind),2),ExpIDs(ind),length(adata(ind).ROIs),isfield(adata(ind).ROIs,'activity')))
    curr_load_path=[tmp_data_dir mouse_id '\Data\' date_id '\' mouse_id auxIDs{ind} '.lvd'];
    if calc_output
        aux_data=load_lvd(curr_load_path);
        run_act_tmp(ind)=sum(abs(diff(aux_data(1,:))))+sum(abs(diff(aux_data(6,:))));
        frame_times=get_frame_times(aux_data(2,:));
        tmp(ind).act_code=get_act_code(aux_data,frame_times);
        lp_tmp(ind)=median(aux_data(4,:));
    end
    lp_set_str=inifile([tmp_data_dir mouse_id '\ImagingData\' date_id '\' adata(ind).fnames{1}(1:end-4) '.ini'],'read',{'main','','laser.eom.set'});
    lp_set(ind)=str2num(lp_set_str{1}(2:end-1));
    %     a=load([adata_dir mouse_id '\' date_id '\' adata_file],'ROItrans');
    %     a.ROItrans
    %plot_all_ROIs_activity(adata(ind).ROIs);
end

if calc_output
    for ind=1:length(adata)
        adata(ind).act_code=int8(tmp(ind).act_code);
        adata(ind).laser_power=lp_tmp(ind);
        adata(ind).laser_power_set=lp_set(ind);
    end
end


if vis_output
    hf1=figure;
    set(hf1,'menubar','none')
    colormap gray
    
    for ind=1:3
        axes('position',[1/3*(ind-1) 1/2*(1) 1/3 1/2]);
        imagesc(adata(ind).template);
        if ind==1
            text(.1,.125,'Template','units','normalized','color','w','fontweight','bold','Interpreter','none')
            text(.1,.05,[animalIDtxt ' ' regionIDtxt],'units','normalized','color','w','fontweight','bold','Interpreter','none')
        end
        text(.1,.95,[num2str(tpointIDs(ind)) 'h'],'units','normalized','color','w','fontweight','bold')
        axis off
    end
    
    for ind=1:3
        axes('position',[1/3*(ind-1) 0 1/3 1/2]);
        imagesc(adata(ind).act_map);
        if ind==1
            text(.1,.125,['Activity Map'],'units','normalized','color','w','fontweight','bold','Interpreter','none')
            text(.1,.05,[animalIDtxt ' ' regionIDtxt],'units','normalized','color','w','fontweight','bold','Interpreter','none')
        end
        text(.1,.85,[num2str(tpointIDs(ind)) 'h'],'units','normalized','color','w','fontweight','bold')
        axis off
    end
end



if calc_output
    for ind=1:length(adata)
        curr_ind=find(tpointIDs(ind)==tpoints);
        run_act(curr_ind)=run_act_tmp(ind);
        
        mean_act(curr_ind)=mean(adata(ind).act_map(:));
        mean_template(curr_ind)=mean(adata(ind).template(:));
        
        tmp=adata(ind).act_map.*(adata(ind).template+50)./adata(ind).template;
        bright_pixs=(adata(ind).template>prctile(adata(ind).template(:),25));
        tmp=tmp.*bright_pixs;
        tmp=imerode(tmp>3,ones(3));
        act(curr_ind)=sum(tmp(:));
        %allROIact=NaN;
        sig_act=zeros(length(adata(ind).ROIs(1).activity),length(adata(ind).ROIs));
        r_act_mat=zeros(length(adata(ind).ROIs(1).activity),length(adata(ind).ROIs));
        if isfield(adata(ind).ROIs,'activity');
            for knd=1:length(adata(ind).ROIs)
                r_act_mat(1:length(adata(ind).ROIs(knd).activity),knd)=adata(ind).ROIs(knd).activity;
            end
            parfor knd=1:length(adata(ind).ROIs)
                tmp=find_active_ind(r_act_mat(:,knd),4,mean(adata(ind).template(:))/4);
                %tmp(7000:10000)=0;
                allROIact(curr_ind,knd)=sum(tmp);
                sig_act(:,knd)=tmp;
            end
        end
        %adata(ind).sig_act=logical(sig_act);
        adata(ind).sig_act=sig_act;
        adata(ind).timepoint=tpointIDs(ind);
        adata(ind).act_map=int16(32*adata(ind).act_map);
        adata(ind).template=int16(32*adata(ind).template);
        
        for knd=1:length(adata(ind).ROIs)
            adata(ind).ROItype(knd)=double(adata(ind).ROIs(knd).type);
        end
    end
    adata=rmfield(adata,'ROIs');
else
    allROIact=NaN;
    act=NaN;
    run_act=NaN;
    act_code=NaN;
end


% figure
% for ind=1:length(adata(1).ROIs)
%     tmp=zeros(size(adata(1).template));
%     for knd=1:length(adata)
%         tmp(adata(knd).ROIs(ind).indices)=knd;
%     end
%     imagesc(tmp);
%     drawnow
%     pause
% end
















